package com.cmw.auntapp.core.util;


import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


public class StringUtils {


    public static boolean isEmpty(Object obj) {

        return null == obj || "".equals(obj.toString().trim());
    }

    public static boolean isNull(Object obj) {

        return null == obj || "".equals(obj.toString().trim()) || "null".equals(obj);
    }

    public static boolean isBlank(Object obj) {

        return null == obj || "".equals(obj.toString());
    }


    public static boolean isNotEmpty(Object obj) {

        return !isEmpty(obj);
    }

    public static String getSequenceId() {
        String mark = String.valueOf(System.currentTimeMillis());
        return mark;
    }

    public static String getCurrentDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        return dateFormat.format(new Date());
    }

    public static String transformDateTime(long t) {
        Date date = new Date(t);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String transformDateTime(long t, String fmt) {
        Date date = new Date(t);
        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
        return dateFormat.format(date);
    }

    public static String transformDateTime(String t, String fmt) {
        Date date = new Date(Long.valueOf(t));
        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
        return dateFormat.format(date);
    }

    public static String getCurrentlyDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        return dateFormat.format(new Date());
    }


    public static String getUUID() {

        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String generate(String imageUri) {
        byte[] md5 = getMD5(imageUri.getBytes());
        BigInteger bi = new BigInteger(md5).abs();
        return bi.toString(10 + 26);
    }

    private static byte[] getMD5(byte[] data) {
        byte[] hash = null;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(data);
            hash = digest.digest();
        } catch (NoSuchAlgorithmException e) {

        }
        return hash;
    }

    /**
     * 从异常中获取有用信息
     *
     * @param ex
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getExceptionMsg(Throwable ex) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        Throwable cause = ex.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        return result;
    }

    /**
     * 如果data为null或者"' 返回"" 否则返回本身
     *
     * @param data
     * @return
     */
    public static String getDataOrEmpty(String data) {
        if (isEmpty(data)) return "";
        return data;
    }

    /**
     * 如果data为null或者"' 返回"" 否则返回本身
     *
     * @param data
     * @return
     */
    public static String getNumber(String data) {
        if (isEmpty(data)) return "0";
        return data;
    }


    public static String getRandomString(int length) {
        //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getMinuteStr(int time) {
        //length表示生成字符串的长度
        String result = "";
        if (time <= 0) {
            return "00:00";
        }
        int minute = time / 60;
        int second = time % 60;
        if (minute < 10) {
            result += "0" + minute + ":";
        } else {
            result += minute + ":";
        }

        if (second < 10) {
            result += "0" + second;
        } else {
            result += second;
        }
        return result.trim();
    }

}
